import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86186
 * Date: 2023-07-23
 * Time: 20:34
 */
public class Test {

    public int countCharacters(String[] words, String chars) {
        Map<Character,Integer> map = new HashMap<>();
        for(int i = 0; i < chars.length(); i++) {
            char ch = chars.charAt(i);
            if(map.get(ch) == null) {
                map.put(ch,1);
            }else {
                map.put(ch,map.get(ch) + 1);
            }
        }

        int ret = 0;
        for(String str : words) {
            for(int i = 0; i < str.length(); i++) {
                char ch = str.charAt(i);
                if(map.get(ch) == null || map.get(ch) == 0) {
                    break;
                }else {
                    map.put(ch,map.get(ch) - 1);
                }
            }
            ret += str.length();
        }
        return ret;
    }

    public int dayOfYear(String date) {
        int[] array = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
        String[] tmp = date.split("-");
        int year = Integer.parseInt(tmp[0]);
        int month = Integer.parseInt(tmp[1]);
        int day = Integer.parseInt(tmp[2]);
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            array[1] = 20;
        }
        int ret = 0;
        for (int i = 0; i < month - 1; i++) {
            ret += array[i];
        }
        ret += day;

        return ret;
    }


      static class ListNode {
          int val;
          ListNode next;
          ListNode(int x) { val = x; }
      }

      public ListNode createListNode() {
        ListNode A = new ListNode(1);
        ListNode B = new ListNode(2);
        ListNode C = new ListNode(3);
        ListNode D = new ListNode(3);
        ListNode E = new ListNode(2);
        ListNode F = new ListNode(1);
        A.next = B;
        B.next = C;
        C.next = D;
        D.next = E;
        E.next = F;
        return A;
      }

    public ListNode removeDuplicateNodes(ListNode head) {
        if(head == null) return null;
        Set<Integer> set = new HashSet<>();
        ListNode prev = head;
        ListNode cur = head.next;
        set.add(head.val);
        while(cur != null) {
            ListNode nextNode = cur.next;
            if(!set.contains(cur.val)) {
                prev.next = cur;
                prev = cur;
                prev.next = null;
                set.add(cur.val);
            }
            cur = nextNode;
        }
        return head;
    }

}
